<?php

namespace app\controller\admin\run;

use app\model\admin\run\CategoryGood;
use laytp\controller\Backend;
use laytp\library\Tree;
use think\Db;

/**
 * Class Category
 * @package app\controller\admin\run
 */
class Category extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $game_model;
    protected $good_model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {
        $this->model = new \app\model\admin\run\Category();
        $this->good_model = new CategoryGood();
        $this->game_model = new \app\model\admin\run\Game();
    }


    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $sourceData = $this->model->where($where)->order($order)->select()->toArray();
        $gameName = $this->game_model->column('name','id');
        array_walk($sourceData,function(&$item) use ($gameName){
           $item['game_name'] = $gameName[$item['game_id']];
        });
        $isTree = $this->request->param('is_tree');
        if($isTree){
            $menuTreeObj = Tree::instance();
            $menuTreeObj->init($sourceData);
            $data = $menuTreeObj->getRootTrees();
        }else{
            $data = $sourceData;
        }
        return $this->success('数据获取成功', $data);
    }

    //查看详情
    public function prop()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);

        $info['good_ids'] = $this->good_model->where(array('category_id' => $id))->column('good_id');
        return $this->success('获取成功', $info);
    }


    public function saveProp(){
        $category_id   = $this->request->param('id');
        $game_id   = $this->request->param('game_id');
        $prop_ids   = $this->request->param('prop_ids');

        try {

            $prop_ids = explode(',',$prop_ids);
            if(empty($prop_ids) || empty($game_id) || empty($category_id)){
                throw new \Exception('参数不正确');
            }

            \think\facade\Db::startTrans();
            $delete_res = $this->good_model->where(array('game_id'=> $game_id,'category_id'=> $category_id))->delete();
            $insData = [];
            foreach($prop_ids as $good_id){
                $insData[] = array(
                    'game_id' => $game_id,
                    'category_id' => $category_id,
                    'good_id' => $good_id
                );
            }

            $ins_res = $this->good_model->insertAll($insData);
            if($delete_res === false || $ins_res === false){
                \think\facade\Db::rollback();
                throw new \Exception('保存失败');
            }
            \think\facade\Db::commit();
            return $this->success('保存成功');
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }

    }
}